package com.sgb.logistics.startup;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.startup.Initializer;

import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.sgb.net.BuildConfig;
import com.sgb.net.core.NetConfig;
import com.swgk.core.base.model.factory.CookieInterceptor;
import com.swgk.core.common.utils.CommonInitializer;
import com.swgk.core.customview.MyLoadFooter;
import com.swgk.core.customview.MyRefreshHeader;
import com.swgk.core.util.SharedPreferenceUtil;
import com.tencent.mmkv.MMKV;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 杨畔
 * @date: 2021/10/18 15:15
 * @Description: 物流启动器
 */
public class LogisticsInitializer implements Initializer<Object> {

    @NonNull
    @Override
    public Object create(@NonNull Context context) {
        init((Application) context);
        return null;
    }

    @NonNull
    @Override
    public List<Class<? extends Initializer<?>>> dependencies() {
        //添加依赖组件启动器
        List<Class<? extends Initializer<?>>> list = new ArrayList<>();
        list.add(CommonInitializer.class);
        return list;
    }

    /**
     * 初始化
     */
    private void init(Application application) {
        //初始化数据存储
        MMKV.initialize(application);
        SharedPreferenceUtil.init(application);
        //添加刷新控件统一头部
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> new MyRefreshHeader(context));
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> new MyLoadFooter(context));
        //配置网络请求
        NetConfig.config()
                .baseUrl(BuildConfig.BASE_URL)
                //token验证拦截器
                .addInterceptor(new CookieInterceptor());
    }
}
